/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controlPlaylisted;

import guiPlaylisted.PlaylistedApp;

import java.io.FileNotFoundException;
import java.io.IOException;

import dataPlaylisted.Music;

import Response.ResponseMusic;
import Request.RequestMusic;

import dataBasePlaylisted.getMusicListCommand;
import dataBasePlaylisted.setMusicListCommand;

import dataPlaylisted.Styles;
import dataPlaylisted.Cammelot;

import java.util.HashMap;
import java.util.ArrayList;

/**
 *
 * @author Administrador
 */
public class MusicController {

    private static MusicController instance;

    private ArrayList<Music> m_musicList;

    private Styles m_styles;

    private Cammelot m_cammelot;

    private HashMap<String, ControlMusicCommand> m_mapCommand;

    public MusicController() throws FileNotFoundException, IOException, ClassNotFoundException{
        m_musicList = new ArrayList<Music>();
        m_styles = new Styles();
        m_cammelot = new Cammelot();
        m_mapCommand = new HashMap();
        this.initCommand();
    }

    public static MusicController getInstance() throws FileNotFoundException, IOException, ClassNotFoundException{
        instance = PlaylistedApp.getMusicCtrl();
        if(instance == null)
            instance = new MusicController();
        return instance;
    }

    public ArrayList<Music> getMusicList(){
        return m_musicList;
    }

    public ArrayList<String> getStyleList(){
         return m_styles.getStyles();
     }

     public ArrayList<String> getCammelotList(){
         return m_cammelot.getCammelotWheel();
     }

    public void loadMusicList() throws ClassNotFoundException{

        RequestMusic req;
        ResponseMusic resp;

        req = new RequestMusic();
        resp = new ResponseMusic();

        req.setParameter("loadMusicList");

        this.doPost(req, resp);

        m_musicList = resp.getObjectList();
    }

    private void saveMusicList() throws FileNotFoundException, ClassNotFoundException, IOException{

        RequestMusic req;
        ResponseMusic resp;

        req = new RequestMusic();
        resp = new ResponseMusic();

        req.setObjectList(m_musicList);

        req.setParameter("saveMusicList");

        this.doPost(req, resp);
    }

     public void setMusicOnList(String _nameTrack, String _nameArtist, String _cammelot, String _style) throws FileNotFoundException, ClassNotFoundException, IOException{

        Music _music;
        _music = new Music();

        _music.setArtistName(_nameArtist);
        _music.setTrackName(_nameTrack);
        //_music.setPair(_style, _cammelot);

        _music.setCammelot(_cammelot);
        _music.setStyle(_style);

        m_musicList.add(_music);

        this.saveMusicList();
    }

     private void initCommand() throws FileNotFoundException, IOException, ClassNotFoundException{
        m_mapCommand.put("loadMusicList", new getMusicListCommand());
        m_mapCommand.put("saveMusicList", new setMusicListCommand());
     }


     private void doPost(RequestMusic req, ResponseMusic resp) throws ClassNotFoundException{
        m_mapCommand.get(req.getParameter()).execute(req, resp);
     }


}
